package ar.unlam.com.tateti.blackboard;

public class Blackboard {

	public  int nroJugadas;
	private int table[][]= new int[3][3];
	private boolean gameOver;
	
	
	
	public Blackboard(){
		initialize();
	}
	public void initialize(){
		nroJugadas=0;
		table[0][0]=-1;
		table[0][1]=-1;
		table[0][2]=-1;
		table[1][0]=-1;
		table[1][1]=-1;
		table[1][2]=-1;
		table[2][0]=-1;
		table[2][1]=-1;
		table[2][2]=-1;		
		setGameOver(false);
	}
	
	public void update(int i,int j,int id){
		nroJugadas ++;
		table[i][j]=id;
		if (hayGanador(i,j,id)){
			this.setGameOver(true);
		}
	}
	
	public boolean inspect(int i, int j, int id){			
		if  ((!isGameOver()) && (table[i][j] == -1))
			return true;		
		else
			return false;    	
	}
	
	private boolean hayGanador(int i,int j,int id){
		
		if (nroJugadas >= 5){   //empiezo a controlar si hay ganador despues de la 5ta jugada
			if ((solucion1(id)) || (solucion2(id)) || (solucion3(id)) || (solucion4(id)) || (solucion5(id))
					|| (solucion6(id)) || (solucion7(id)) || (solucion8(id)))
				return true;
			else 
				return false;
		}else
			return false;
	}
			
	
	private boolean solucion1(int id){
		if ((table[0][0]==id)&&(table[1][0]==id)&&(table[2][0]==id)){
			System.out.println("solucion");
			return true;}
		else 
			return false;
	}	
	private boolean solucion2(int id){
		if ((table[1][0]==id)&&(table[1][1]==id)&&(table[1][2]==id))
			return true;
		else 
			return false;
	}	
	private boolean solucion3(int id){
		if ((table[2][0]==id)&&(table[2][1]==id)&&(table[2][2]==id))
			return true;
		else 
			return false;
	}	
	private boolean solucion4(int id){
		if ((table[0][2]==id)&&(table[1][2]==id)&&(table[2][2]==id))
			return true;
		else 
			return false;
	}	
	private boolean solucion5(int id){
		if ((table[1][0]==id)&&(table[1][1]==id)&&(table[1][2]==id))
			return true;
		else 
			return false;
	}
	private boolean solucion6(int id){
		if ((table[0][0]==id)&&(table[0][1]==id)&&(table[0][2]==id))
			return true;
		else 
			return false;
	}	
	private boolean solucion7(int id){
		if ((table[0][0]==id)&&(table[1][1]==id)&&(table[2][2]==id))
			return true;
		else 
			return false;
	}	
	private boolean solucion8(int id){
		if ((table[0][2]==id)&&(table[1][1]==id)&&(table[2][0]==id))
		                                                 
			return true;
		else 
			return false;
	}
	public boolean isGameOver() {
		return gameOver;
	}
	public void setGameOver(boolean gameOver) {
		this.gameOver = gameOver;
	}	
}



